﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="msg_send_usersel.aspx.cs" Inherits="Ccit.SysPlat.Remind.msg_send_usersel" %>

<%@ Register Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="Microsoft.Web.UI.WebControls" TagPrefix="iewc" %>
    
<%@ Register TagPrefix="ccit" Namespace="Ccit.ServerControls" Assembly="Ccit.ServerControls.ActionForm" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>在线撰写-选择用户</title>
    <script language="javascript" src="../script/jquery.js" type="text/javascript"></script>
    <script language="javascript" src="../script/StringClass_Ext.js" type="text/javascript"></script>
    <script language="javascript" src="../script/jsonlib.js" type="text/javascript"></script>
</head>
<body>
    <ccit:ActionForm id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    <!--绑定树的标志-->
    <input type="hidden" id="_hidFlag" runat="server" />
    <!--组织机构的根节点id-->
    <input type="hidden" id="_hidRootId" runat ="server" />
    
    <!--选中节点id-->
    <input type="hidden" id="_hidSelNodeId" runat ="server" />
    <!--判断选中节点是否有子节点-->
    <input type="hidden" id="_hidHaveChild" runat ="server" />
    <!--选中节点类型-->
    <input type="hidden" id="_hidNodeType" runat ="server" />
    <!--找到的节点id-->
    <input type="hidden" id="_hidFindId" runat ="server" />
    
    <input type="hidden" id="_hidLevelStruts" runat ="server" />
    
    <input type="hidden" id="_hidExpandSign" runat="server" />
    
    <iewc:TreeView ID="tvUser" runat="server" onclick="selectedNode()">
    </iewc:TreeView>
  
    <input type="button" id="btnTrigger" onclick="doTrigger()" style="display: none" value="触发事件" />
    
    <script type="text/javascript" language="javascript">
      var node = null;
      
      function selectedNode()
      {
        var nodeIndex = document.getElementById("tvUser").clickedNodeIndex;
        var node = document.getElementById("tvUser").getTreeNode(nodeIndex);
        if(node == null) return ;
        var vNodeId = node.getAttribute("ID");
        var vNodeArray = vNodeId.split("_");
        var vPrimaryId = vNodeArray[0];
        var vExpandSign = vNodeArray[1];
        var vType = vNodeArray[2]
        
        var hasChild =( node.getChildren().length > 0) ? "Y" : "N"; 
        document.getElementById("_hidSelNodeId").value = vPrimaryId;
        document.getElementById("_hidHaveChild").value = hasChild;
        document.getElementById("_hidNodeType").value = vType;
        
      }
      
      function tvUser.onexpand()
      {
          var nodeIndex = event.treeNodeIndex;
          if(typeof(nodeIndex) == "undefined")
          {
             node = null;
             return false;
          }
          tvUser.selectedNodeIndex = nodeIndex;
          node = tvUser.getTreeNode(nodeIndex);
          
          var vNodeId = node.getAttribute("ID");
          var vNodeArray = vNodeId.split("_");
          var vPrimaryId = vNodeArray[0];
          var vExpandSign = vNodeArray[1];
          var vType = vNodeArray[2]
          
          var hasChild =( node.getChildren().length > 0) ? "Y" : "N"; 
          document.getElementById("_hidSelNodeId").value = vPrimaryId;
          document.getElementById("_hidHaveChild").value = hasChild;
          document.getElementById("_hidNodeType").value = vType;
          
          
          if(vExpandSign == "0" && vPrimaryId != "0")
          {
             var oSubNodes = node.getChildren();
             var oSubNode;
             for(var i = oSubNodes.length - 1; i >= 0 ; i --)
             {
                oSubNode = oSubNodes[i];
                oSubNode.remove();
             }
             
             var pageFlag = $get("_hidFlag").value;
             
             if(pageFlag == "1") //组织机构
             {
                var levelStruts = document.getElementById("_hidLevelStruts").value;
                var organId = document.getElementById("_hidRootId").value;
                PageMethods.GetOrganSubNodes(vPrimaryId, nodeIndex, levelStruts, organId,ClientCallBack);
             }
             if(pageFlag == "2") //在线人员
             {
                PageMethods.GetUserSubNodes(vPrimaryId,nodeIndex,ClientCallBack);
             }
             if(pageFlag == "3") //近期联系人
             {
                PageMethods.GetRecentSubNodes(vPrimaryId,nodeIndex,ClientCallBack);
             }
          }
      }
      function ClientCallBack(vResult)
      {
          if(vResult == null || vResult == "") return;

          eval("var resultObj=" + vResult);
          var operType = resultObj.operType;
          switch(operType)
          {
              case "ORGAN":
                  var nodeIndex = resultObj.nodexIndex;
                  var lstUser = resultObj.lstUser;
                  var lstDept = resultObj.lstDept;
                  var levelStruct = resultObj.levelStruts;
                  var parentNodeId = resultObj.upDeptId;                  
                  createOrganSubTreeNodes(nodeIndex, lstDept, lstUser);
                  var expandSign = document.getElementById("_hidExpandSign").value;
                  if (expandSign == "1") {    //是逐级展开
                      var parentIdArr = levelStruct.split(",");
                      var iLen = parentIdArr.length;
                      var bGet = false;
                      var findNodeIndex;

                      for (var i = 0; i < iLen; i++) {
                          var colId = parentIdArr[i];
                          var childArray = colId.split("~");
                          var setCtrl;
                          setCtrl = findTreeNode(childArray[0], childArray[1]);
                          findNodeIndex = setCtrl.getNodeIndex();
                          if (i < iLen - 1) {
                              if (!bGet) {
                                  if (childArray[0] == parentNodeId) {
                                      if (i == (iLen - 1)) {   //如果到最底层,则逐级展开标志0
                                          $get("_hidExpandSign").value = "0";
                                      }
                                      bGet = true;
                                      continue;
                                  }
                              }
                              else //true,展开下级
                              {
                                  var ctrlNode;
                                  ctrlNode = findTreeNode(childArray[0], childArray[1]);
                                  var nodeAttrId = ctrlNode.getAttribute("ID");
                                  var vNodeIdArray = nodeAttrId.split("_");
                                  if (vNodeIdArray[1] == "1") {
                                      if (i == (iLen - 1)) {
                                          ctrlNode.setAttribute("EXPANDED", true);
                                      }
                                      else
                                          continue;
                                  }
                                  else {
                                      ctrlNode.setAttribute("EXPANDED", true);
                                      break;
                                  }
                                  var selId = parentIdArr[iLen - 1];
                                  var setNodeArray = selId.split("~");
                                  var selCtrl = findTreeNode(setNodeArray[0], setNodeArray[1]);
                                  findNodeIndex = selCtrl.getNodeIndex();
                              }
                          }
                      }
                      document.getElementById("tvUser").selectedNodeIndex = findNodeIndex;
                  }
                  break;
              case "USER":
                  var nodeIndex = resultObj.nodeIndex;
                  var lstUser = resultObj.lstUser;
                  for (var i = 0; i < lstUser.length; i++) {
                      var userObj = lstUser[i];
                      createUserSubNodes(nodeIndex, userObj);
                  }
                  break;
              case "EXPANDNODE":
                  var levelStruct = resultObj.levelStruts;
                  //var nodeType = resultObj.nodeType;
                  document.getElementById("_hidLevelStruts").value = levelStruct;
                  var primaryId = document.getElementById("_hidSelNodeId").value;
                  if (primaryId == "0" || primaryId == "0000") {
                      levelStruct = "0000~dept," + levelStruct;
                  }
                  var parentIdArr = levelStruct.split(",");
                  var iLen = parentIdArr.length;

                  var colId = "";
                  var ctrlNode;
                  var nodeAttrId = "";
                  for (var i = 0; i < iLen; i++) {
                      colId = parentIdArr[i];
                      var childArray = colId.split("~");

                      if (childArray[1] == "user") {
                          ctrlNode = findTreeNode(childArray[0], "user");
                      } else {
                          ctrlNode = findTreeNode(childArray[0], "dept");
                      }

                      nodeAttrId = ctrlNode.getAttribute("ID");

                      var vNodeIdArray = nodeAttrId.split("_")
                      if (vNodeIdArray[1] == "1") {    //判断最老祖宗是否展开过 1--：展开过
                          continue;
                      }
                      else {
                          ctrlNode.setAttribute("EXPANDED", true); //没展开连续展开调tvUser.onexpand()方法展开最老祖宗
                          break;
                      }
                  }
                  break;
            }
          resultObj = null;
      }
      
      /* 创建组织机构下的子节点 */
      function createOrganSubTreeNodes(nodeIndex,lstDept,lstUser)
      {      
         var node = $get("tvUser").getTreeNode(nodeIndex);
         var pChecked = node.getAttribute("checked");
         
         var deptObj;
         var userObj;
         var oSubNode;
        
         for (var i = 0; i < lstDept.length; i++) {
             deptObj = lstDept[i];                 
             oSubNode = $get("tvUser").createTreeNode();
             oSubNode.setAttribute("ID", deptObj.deptId + "_0_dept");
             oSubNode.setAttribute("Text", deptObj.deptName);
             oSubNode.setAttribute("checkBox", "true");
             if (pChecked == true) {
                 oSubNode.setAttribute("checked", "true");
             }
             if (deptObj.isHasChild == "1" || deptObj.includeUsers.key != "") {
                 var oEmptyNode = $get("tvUser").createTreeNode();
                 oSubNode.add(oEmptyNode);
             }
             node.add(oSubNode);
         }

         for (var i = 0; i < lstUser.length; i++) {
             userObj = lstUser[i];
             oSubNode = $get("tvUser").createTreeNode();
             oSubNode.setAttribute("ID", userObj.userId + "_0_user");
             oSubNode.setAttribute("Text", userObj.userName);
             oSubNode.setAttribute("checkBox", "true");
             if (pChecked == true) {
                 oSubNode.setAttribute("checked", "true");
             }
             if (userObj.onlineSign == "0")   //离线
                 oSubNode.setAttribute("DefaultStyle", "color:red;");
             else if (userObj.onlineSign == "1") //在线
                 oSubNode.setAttribute("DefaultStyle", "color:blue;");

             node.add(oSubNode);
         }
         
         var vNodeId = node.getAttribute("ID");
         var vArray = vNodeId.split("_");
         
         node.setAttribute("ID",vArray[0] + "_1_dept");
      }
      
      function createUserSubNodes(nodeIndex,userObj)
      {
         var node = $get("tvUser").getTreeNode(nodeIndex);
         var oSubNode;
         oSubNode = $get("tvUser").createTreeNode();
         oSubNode = $get("tvUser").createTreeNode();
         oSubNode.setAttribute("ID", userObj.userId + "_0_user");
         if (userObj.belongDept.deptName == "")
             oSubNode.setAttribute("Text", userObj.userName);
         else
             oSubNode.setAttribute("Text", userObj.userName + "(" + userObj.belongDept.deptName + ")");
         oSubNode.setAttribute("checkBox","true");

         var onlineSign = userObj.onlineStatus;
         if (onlineSign == "0")   //离线
           oSubNode.setAttribute("DefaultStyle","color:red;");
         else if (onlineSign == "1") //在线
           oSubNode.setAttribute("DefaultStyle","color:blue;");
           
          node.add(oSubNode);
          
          var vNodeId = node.getAttribute("ID");
          var vArray = vNodeId.split("_");
          node.setAttribute("ID",vArray[0] + "_1_user");
       }
      
      
        /* oncheck事件 */
        function tree_oncheck(tree)
        {
             var node=tree.getTreeNode(tree.clickedNodeIndex);
             var Pchecked=tree.getTreeNode(tree.clickedNodeIndex).getAttribute("checked");
             setcheck(node,Pchecked);
        }
        
        /* 设置子节点选中 */
        function setcheck(node,Pc)
        {
         var i;
         var ChildNode=new Array();
         ChildNode=node.getChildren();
         
         if(parseInt(ChildNode.length)==0)
                    return;
         else
         {
           for(i=0;i<ChildNode.length;i++)
           {
             var cNode;
             cNode=ChildNode[i];
             if(parseInt(cNode.getChildren().length)!=0)
             setcheck(cNode,Pc);
             cNode.setAttribute("checked",Pc);
           }
          }
        } 
        
         var cNodeIds="";
        /* 获取所有选中节点 */
         function FindCheckedFromNode(node)
         {
            var nodes = new Array();
              
            if(node==null){
               var node=tvUser.getTreeNode("0");   
               nodes = node.getChildren();
            }
            else
            {
               nodes = node.getChildren();
            }
            for (var i = 0; i < nodes.length; i++) {
              var cNode=nodes[i];
              if (cNode.getAttribute("checked")){
                 var checkNodeId=cNode.getAttribute("ID");
                   if(checkNodeId != "")
                        cNodeIds += (cNodeIds == "" ) ? checkNodeId : "," + checkNodeId;
            }
            if (parseInt(cNode.getChildren().length) != 0 )
                  FindCheckedFromNode(cNode);
            }
        }
        
        function GetcheckUserIds()
        {
            FindCheckedFromNode();
            return cNodeIds;
        }
        
        
        /* 循环展开节点 */
        function doTrigger() {
            var oFindId = document.getElementById("_hidFindId").value;
            var nodeType = document.getElementById("_hidNodeType").value;
            var ctrlNode = findTreeNode(oFindId,nodeType);
            if(null == ctrlNode) {    
		         expandNode(oFindId,nodeType);
		         return;
		    }		    
		    
		    var findNodeIndex = ctrlNode.getNodeIndex();
		   // var parentNodeArr = [];   //展开其父及节点(二)
		   // parentNodeArr.push(ctrlNode);
		   // getParentNode(ctrlNode,parentNodeArr);
		    //   expandParentNode(parentNodeArr);
		    var nodeType = document.getElementById("_hidNodeType").value;
		    if(nodeType == "organ")
		        expandParent(ctrlNode); //展开父节点(一)
		   
		    document.getElementById("tvUser").selectedNodeIndex = findNodeIndex;
        }
        
        /* 获取上级父节点,将其展开 */
        
        // 从最里面开始展开(一)
		function expandParent(dtNode)
		{
		    var parNode = dtNode.getParent();
		   parNode.setAttribute("EXPANDED",true);
		   if(parNode.getNodeIndex() != "0")
		      expandParent(parNode);   
		}

        // 从最外面开始展开	(二)
//		function getParentNode(ctrlNode,parentNodeArr)
//		{ 
//		   var parNode = ctrlNode.getParent();
//		   parentNodeArr.push(parNode);
//		   if(parNode.getNodeIndex() != "0") {		      
//		      getParentNode(parNode,parentNodeArr);
//		   }
//		}
		
//		function expandParentNode(parentNodeArr) {
//		    if (null != parentNodeArr && parentNodeArr != "undefined") {
//		        var itemCount = parentNodeArr.length;
//		        var node;
//		        for(var i = itemCount - 1; i >= 0; i --) {
//		            node = parentNodeArr[i];
//		            node.setAttribute("EXPANDED",true);
//		        }
//		    }
//		}
        
        function expandNode(dtNodeId,nodeType) {
		    $get("_hidExpandSign").value = "1";//连续展开标志
		    PageMethods.GetNodeLevelStruct(dtNodeId,nodeType,ClientCallBack);  //获取新父节点上级节点
		}
      
        /* 返回查到的节点对象 */
        function findTreeNode(oFindId,nodeType) {
            var ctrlTree = document.getElementById("tvUser");		    
		    
		    var ctrlRoot = ctrlTree.getTreeNode("0"); //获取根节点
		    var ctrlChildNodes = ctrlRoot.getChildren();  //所有子节点
	    
		    var ctrlFindNode = recursionFind(ctrlChildNodes,oFindId,nodeType);
            return ctrlFindNode;
        }
        
        /* 循环查找返回查到的节点对象 */
        function recursionFind(ctrlChildNodes,oFindId,nodeType) {
            var iLen = ctrlChildNodes.length;
            var ctrlTreeNode;
		    var vNodeId;
		    var vNodeIdArray;
		    var vAstTypeId;
		    var ctrlSubChildNodes;
		    var bFind = false;
		    
            for(var i = 0; i < iLen; i ++) {
		        ctrlTreeNode = ctrlChildNodes[i];
		        vNodeId = ctrlTreeNode.getAttribute("ID");    
		        
		        var oFindTreeId0 = oFindId +"_0_" + nodeType;
		        var oFindTreeId1 = oFindId +"_1_" + nodeType;
		        if(vNodeId == oFindTreeId0 || vNodeId == oFindTreeId1){		           
		            bFind = true;
		            break;       
		        }
		        else {
		            ctrlSubChildNodes = ctrlTreeNode.getChildren();
		            ctrlTreeNode = recursionFind(ctrlSubChildNodes,oFindId,nodeType);
		            if(null != ctrlTreeNode) {
		                bFind = true;
		                break;
		            }
		        }
		    }
            
            if(bFind)
                return ctrlTreeNode;
            else
                return null;
        }
        
    </script>
    </ccit:ActionForm>
</body>
</html>
